package com.zxt.security.service;


import com.zxt.security.bean.LoginUser;
import com.zxt.security.bean.SysUser;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Objects;

/**
 * 用户验证处理
 */
@Service
@Slf4j
@AllArgsConstructor
public class UserDetailsServiceImpl implements UserDetailsService {

    //private final BUserManager bUserManager;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser user = new SysUser();
        user.setUserName("admin");
        user.setPassword("$2a$10$8ls3oCG5kFrz4dPdZdpBce.if.vlNDH7nI09hgXNvssGO71Q0rCxe");
        if (Objects.isNull(user)) {
            // 用户不存在
            throw new UsernameNotFoundException("用户不存在");
        }
        return createLoginUser(user);
    }

    public UserDetails createLoginUser(SysUser user) {
        LoginUser loginUser = new LoginUser();
        loginUser.setUser(user);
        return loginUser;
    }
}
